
#include "xmodule_config.h"

#if MODULE_CHECK_ENABLED( AUTO_K )
#include "em_autok.h"

#include "rtl876x_rcc.h"
#include "rtl876x_gpio.h"
#include "rtl876x_pinmux.h"

#include "boards.h"

#if MODULE_CHECK_LOG_ENABLED( AUTO_K )
#include "log.h"
#define LOG     LOG_INFO
#define LOG0    LOG_INFO0 
#define LOG1    LOG_INFO1
#define LOG2    LOG_INFO2
#define LOG3    LOG_INFO3
#else 
#define LOG(...) 
#define LOG0(...)
#define LOG1(...)
#define LOG2(...)
#define LOG3(...)
#endif 

static void gpio_configs(void)
{
    RCC_PeriphClockCmd( APBPeriph_GPIO, APBPeriph_GPIO_CLOCK, ENABLE );
    
    Pad_Config( COMMON_SCL_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE, PAD_OUT_HIGH );
    Pad_Config( COMMON_SDA_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE, PAD_OUT_HIGH );
    Pinmux_Config( COMMON_SCL_PIN, DWGPIO );
    Pinmux_Config( COMMON_SDA_PIN, DWGPIO ); 
    
    
    GPIO_InitTypeDef gpioInitStruct;
    GPIO_StructInit( &gpioInitStruct );
    gpioInitStruct.GPIO_Pin         = GPIO_GetPin( COMMON_SCL_PIN );
    gpioInitStruct.GPIO_Mode        = GPIO_Mode_IN;
    gpioInitStruct.GPIO_ITCmd       = DISABLE;
    GPIO_Init( &gpioInitStruct );

	GPIO_StructInit( &gpioInitStruct );
    gpioInitStruct.GPIO_Pin         = GPIO_GetPin( COMMON_SDA_PIN );
    gpioInitStruct.GPIO_Mode        = GPIO_Mode_IN;
    gpioInitStruct.GPIO_ITCmd       = DISABLE;
    GPIO_Init( &gpioInitStruct );     
}

void em_auto_k_init(void)
{
    gpio_configs();
}

bool em_auto_k_check_actived(void)
{
    return (GPIO_ReadInputDataBit( GPIO_GetPin(COMMON_SCL_PIN) ) == 0)
        || (GPIO_ReadInputDataBit( GPIO_GetPin(COMMON_SDA_PIN) ) == 0)
        ; 
}

#endif
